package org.l.codemaker;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 阿旺
 * @version 3.0.0
 * @FileName Logger.java
 * @description Logger.java
 * @since 2016年3月17日 history 3.0.0 Jan 13, 2015 created by 【阿旺】
 */
public class Logger {

    static JTextArea textArea;

    private static String time() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return format.format(date);
    }

    public static void show(Object object) {
        if (textArea == null) {
            return;
        }

        textArea.append("[" + time() + "] ");
        textArea.append(String.valueOf(object));
        textArea.append("\n");

        int length = textArea.getText().length();
        textArea.setCaretPosition(length);
    }

    public static void show(Object object, Throwable t) {
        show(object + "：" + String.valueOf(t));

        if (textArea != null) {
            t.printStackTrace();
            // StackTraceElement[] stackElements = t.getStackTrace();
            // if (stackElements != null) {
            // for (int i = 0; i < stackElements.length; i++) {
            // System.out.print(stackElements[i].getClassName()+"/t");
            // System.out.print(stackElements[i].getFileName()+"/t");
            // System.out.print(stackElements[i].getLineNumber()+"/t");
            // System.out.println(stackElements[i].getMethodName());
            // }
            // }
        }
    }
}
